# -*-coding:utf-8-*-
import re
from utils import read_txt, cut_sent, read_word, read_pos, read_hed
from Syntactic_Analysis import ParseResult

'''
判断“有”字句
LAC默认模型分词 粒度太大 如：我没有钱---> 我没有  钱
换用北大分词工具 pkuseg LAC标注词性
分析"有"字句的句法结构
type_lst = ['主||“（没）有”+宾', '主||“（没）有”+数量词组', '主||“有”+[状语]+形/动', '主||[状语]+“（没）有"', '非”有“字句']
两种情形：
1. HED为“有”  确定修饰HED的SBV和VOB     '主||“（没）有”+宾', '主||“（没）有”+数量词组', '主||[状语]+“（没）有"’
2. HED为其他动词或者形容词  确定作为ADV或VOB的修饰HED的“有”     '主||“有”+[状语]+形/动'
'''


def find_key(sentence):
    # 正则表达式寻找关键字 确定句子中含“有”
    pattern_1 = re.compile(r'[\u6709]')  # 有
    if re.findall(pattern_1, sentence):
        return sentence
    return ""


def judge_type(hed_info, modifi_info):
    sbv_info = modifi_info['sbv_info']  # SBV成分
    vob_info = modifi_info['vob_info']  # VOB成分
    adv_info = modifi_info['adv_info']  # ADV成分
    att_info = modifi_info['att_info']  # ATT成分
    sbv_h = read_hed(sbv_info, hed_info['word'])  # 修饰HED的SBV成分
    vob_h = read_hed(vob_info, hed_info['word'])  # 修饰HED的VOB成分
    adv_h = read_hed(adv_info, hed_info['word'])  # 修饰HED的ADV成分

    type_lst = ['主||“（没）有”+宾', '主||“（没）有”+数量词组',
                '主||“有”+[状语]+形/动', '主||[状语]+“（没）有"', '其他句型']
    verb_lst = ['v', 'vd', 'vn']
    adj_lst = ['a', 'ad', 'an']
    quan_lst = ['m', 'q']
    type_id = len(type_lst) - 1

    # 第一种情形 “有”作为HED 寻找修饰“有”的SBV
    if (find_key(hed_info['word'])) and sbv_h:
        # '主||“（没）有”+数量词组'
        if read_pos(att_info, quan_lst):
            type_id = 1
            return type_lst[type_id]
        # 主||[状语]+“（没）有"
        if adv_h and adv_h['pos'] == 'd':
            type_id = 3
            return type_lst[type_id]
        else:
            # 主||“（没）有”+宾'
            type_id = 0
            return type_lst[type_id]
    # 第二种情形 其他动词或形容词作为HED  寻找作为ADV或VOB的”有“
    elif sbv_h and read_hed(adv_info,hed_info['word']) and read_word(adv_info+vob_info,["没有", "有"]):
        type_id = 2
        return type_lst[type_id]
    return type_lst[type_id]


if __name__ == '__main__':
    path = './data/test_you.txt'
    sentences = read_txt(path)
    for sent in sentences:
        print('*' * 125)
        print(sent)
        fenci_res, pos_res, ddp_res = cut_sent(sent)
        pr = ParseResult(fenci_res, pos_res, ddp_res)
        print('分词：', fenci_res)
        print('词性：', pos_res)
        print('句法：', ddp_res)
        modifi_info = pr.get_modifi_info()
        hed_info = pr.get_hed_info()
        print(judge_type(hed_info, modifi_info))

